import Axios from "axios";
import { ElMessage } from "element-plus";

const baseURL = ''

const axios = Axios.create({
  baseURL,
  timeout: 5000 // 请求超时5s
})

// 前置拦截器(发起请求之前拦截)
axios.interceptors.request.use(
  (response) => {
    // 设置config
    return response
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 后置拦截器
axios.interceptors.response.use(
  (response) => {
    // 设置response和error
    return response
  },
  (error) => {
    if (error.response && error.response.data) {
      const code = error.response.status
      const msg = error.response.data.message
      ElMessage.error(`Code:${code}，Message: ${msg}`)
    } else {
      ElMessage.error(`${error}`)
    }
    return Promise.reject(error)
  }
)

export default axios